package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.LocalAttrHelper;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.RouteProtocolTypeAttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <Description> <br>
 *
 * @author cheng.leizhen01<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2021年3月14日 <br>
 * @since R9.0<br>
 * @see com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler <br>
 */
@Service
@AttrHandler(RouteProtocolTypeAttrHandler.class)
public class RouteProtocolTypeAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        if (Objects.isNull(attr) || KiteStringUtils.isEmpty(attr.getValue())) {
            return;
        }
        // BGP客户路由是否需要广播 200001564
        // BGP接入客户所需路由  200001565
        // 省网转接与骨干网建立Multi-hop连接 200001566
        // BGP详细方案  200001567
        // 用户设备Loopback地址 200001568
        // BGP Multi-hop跳数 200001569
        // BGP MD5认证码 200001570

        //当路由协议类型值为‘BGP’时，该字段显示，且为必填，否则隐藏
        KiteAttr asOnAttr = inst.getAttr("2_0000157");
        KiteAttr throughModelAttr = inst.getAttr("2_0000067");
        if (Objects.isNull(throughModelAttr) || Objects.isNull(asOnAttr)) {
            return;
        }
        if(KiteStringUtils.equals("1_0000213", attr.getValue())) {
            asOnAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
            asOnAttr.setIsNull("F");
            LocalAttrHelper.showAndNotNull(inst, "200001566");
            LocalAttrHelper.showForDAndNull(inst,"200001564","200001565","200001567");
        } else {
            asOnAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);
            asOnAttr.setIsNull("T");
            LocalAttrHelper.hideAndNullable(inst, "200001564","200001565","200001566","200001567","200001568","200001569","200001570");
        }

        //当路由协议值为‘BGP’时，该值枚举为：全穿透、国内穿透、非穿透；当路由协议为‘静态’时，默认值为‘无’
        if(KiteStringUtils.equals("1_0000213", attr.getValue())) {
            throughModelAttr.setDefaultValue("");
            throughModelAttr.setValue("");
            throughModelAttr.setValueDesc("");
            throughModelAttr.getAttrValues().removeIf(p -> KiteStringUtils.isEqual("1_0000260", p.getAttrValue()));
        } else {
            String attrValues = throughModelAttr.getAttrValues().toString();
            if(!attrValues.contains("1_0000260")){
                KiteAttrValue  kiteAttrValue =  new  KiteAttrValue();
                kiteAttrValue.setAttrId("20210313601");
                kiteAttrValue.setAttrNbr("");
                kiteAttrValue.setAttrValue("1_0000260");
                kiteAttrValue.setAttrValueName("无");
                kiteAttrValue.setAttrValueId("20210313901");
                throughModelAttr.getAttrValues().add(kiteAttrValue);
            }
            throughModelAttr.setDefaultValue("1_0000260");
            throughModelAttr.setValue("1_0000260");
            throughModelAttr.setValueDesc("无");
        }


    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }

}
